<?php
class Default_Model_City extends Zend_Db_Table{

	protected $_name = CITY_TABLE;
	protected $_primary = 'city_id';

	private $CITY_ID = 'city_id';
	private $CITY_NAME = 'name';

	protected $db;

	public function __construct(){
		//Goi ket noi voi Zend Db
		$this->db = Zend_Registry::get('connectDB');
	}

	protected $_messageError = null;

	public function countCity(){
		$select = $this->db->select()
		->from(CITY_TABLE,array('COUNT(city_id) AS totalCity'));
		$result = $this->db->fetchOne($select);
		return $result;
	}

	public function listAllCity(){
		$select = $this->db->select()->from(CITY_TABLE);
		$select->order($this->CITY_NAME.' ASC');
		$result  = $this->db->fetchAll($select);
		return $result;
	}

	public function listAllCityPaging(){
		$select = $this->db->select()->from(CITY_TABLE);
		$select->order($this->CITY_NAME.' ASC');
		//$result  = $this->db->fetchAll($select);
		return $select;
	}
	
	public function getCityInfoById($cityId) {
		$select = $this->db->select()->from(CITY_TABLE)->where($this->CITY_ID.' = ?',$cityId);
		$result  = $this->db->fetchAll($select);
		return $result;
	}

	// insert city
	public function insertCity($arrParam = null){
		try {
			$this->db->insert(CITY_TABLE,$arrParam);
			return true;
		} catch (Exception $e) {
			$this->_messageError = $e->getMessage();
			return false;
		}
	}
	
	// update info
	public function updateCityInfo($arrParam = null, $cityId){
		try {
			$this->db->update(CITY_TABLE, $arrParam, $this->CITY_ID.' = '.$cityId);
			return true;
		} catch (Exception $e) {
			$this->_messageError = $e->getMessage();
			return false;
		}
	}
	
	// Xoa thong tin location
	public function deleteCityById($cityId){
		try {
			$this->db->delete(CITY_TABLE,$this->CITY_ID.' = ' . $cityId);
			return true;
		} catch (Exception $e) {
			$this->_messageError = $e->getMessage();
			return false;
		}
	}

	public function getError(){
		return $this->_messageError;
	}
}